package com.thesis.manage.common.email;

import com.thesis.manage.common.exception.CommonException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.scheduling.annotation.Async;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * @author php
 * @date 2017/12/28
 * 邮件发送
 */
public class EmailSendUtil {
    @Resource
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String springMailUsername;

    /**
     * 简单邮件发送（异步）
     *
     * @param email   接受人邮箱
     * @param subject 主题
     * @param text    内容
     */
    @Async
    public void send(String email, String subject, String text) {
        if (StringUtils.isBlank(email)) {
            throw new CommonException("email is null", "邮箱地址为空");
        }
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(springMailUsername);
        message.setTo(email);
        message.setSubject(subject);
        message.setText(text);
        this.javaMailSender.send(message);
    }

    /**
     * 简单邮件发送（异步）
     *
     * @param email   接受人邮箱
     * @param subject 主题
     * @param text    内容
     */
    @Async
    public void sendHtml(String email, String subject, String text) {
        MimeMessage message;
        try {
            message = javaMailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(springMailUsername);
            helper.setTo(email);
            helper.setSubject(subject);
            helper.setText(text, true);
        } catch (Exception e) {
            throw new CommonException("email send error", "邮件发送失败");
        }
        javaMailSender.send(message);
    }

    /**
     * 发送带有附件的邮件（异步）
     *
     * @param email   接受人邮箱
     * @param subject 主题
     * @param text    内容
     * @param files   附件
     */
    @Async
    public void sendWithAttachment(String email, String subject, String text, File[] files) {
        MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(springMailUsername);
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject(subject);
            mimeMessageHelper.setText(text);
            /*
            如果附件存在，封装附件内容
             */
            if (files.length != 0) {
                FileSystemResource fileSystemResource;
                for (File file : files) {
                    fileSystemResource = new FileSystemResource(file);
                    mimeMessageHelper.addAttachment(fileSystemResource.getFilename(), fileSystemResource);
                }
            }
            this.javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

}
